package com.gxc.yunwei;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.LinkedList;
import java.util.List;
import java.util.Scanner;
import java.util.stream.Collectors;

/**
 * 运维功能 2019.3.18 .
 *
 * @author 宫新程
 * @since 2019/3/18 17:17
 */
public class StockTest {

  public static void main(String[] args) throws Exception {
    List<String> paramList = new LinkedList<>();
    List<String> resultList = new LinkedList<>();

    /** 读取入参列表. */
    FileInputStream fis =
        new FileInputStream(new File("C:\\Users\\Administrator\\Desktop\\read.txt"));
    BufferedReader br = new BufferedReader(new InputStreamReader(fis));
    String line;
    while ((line = br.readLine()) != null) {
      paramList.add(line);
    }
    br.close();
    fis.close();

    /** 是否继续. */
    System.out.print(String.format("读取 %s 条数据，是否继续（Y / N）：", paramList.size() + ""));
    Scanner sc = new Scanner(System.in);
    String next = sc.nextLine();
    if (!"Y".equalsIgnoreCase(next)) {
      System.out.println("退出成功！");
      return;
    }
    sc.close();

    /** 拼装参数. */
    URL url =
        new URL(
            "http://www.yilihuo.com/ylh-cloud-service-goods/api/page/stock/estore-management/receive-delivery-order");

    /** 遍历参数. */
    for (String params : paramList) {
      /** 获取连接. */
      HttpURLConnection conn = (HttpURLConnection) url.openConnection();
      conn.setDoInput(true);
      conn.setDoOutput(true);
      conn.setRequestMethod("POST");
      conn.setRequestProperty("Content-Type", "application/json");
      // 认证token
      conn.setRequestProperty(
          "Authorization",
          "Bearer eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJtZW1iZXJfaWQiOjIxNTUxOSwicmV0YWlsX21lbWJlcl9pZCI6MCwicmV0YWlsX2N1c3RvbWVyX2lkIjowLCJ1c2VyX25hbWUiOiLng73ngavliJvmlrDlrrbnlLUiLCJtZW1iZXJfbmFtZ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wd22gGDxUWgSy9Plu2jTCqsSrKDFaRLkpg-X6OWq4CRWNcYElz7yq1vgEIh3GtnKYiIED0L75D5C3m_qPW8nlHXdLCwFK1yUvW22DgPVnpzXyLVu_SBldIQuYJZi_ApFzMh6DJtI72PJQEwKmkd0Xo9DimXGDN1qv_0NVshEZlvxT0GzGWsGpzxuXZrHFg7rt0qZYG0IsR2JmgpD9NMYVLo8r38S4rF0rWwSIAJX0z6JOP5o1wL8Vk2A");

      OutputStream outputStream = conn.getOutputStream();
      outputStream.write(params.getBytes(StandardCharsets.UTF_8));
      outputStream.flush();

      InputStream in = conn.getInputStream();
      BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(in));
      String body = bufferedReader.lines().collect(Collectors.joining(System.lineSeparator()));
      resultList.add(body);

      // 关闭资源.
      outputStream.close();
      bufferedReader.close();
      conn.disconnect();
    }

    FileOutputStream fos =
        new FileOutputStream(
            "C:\\Users\\Administrator\\Desktop\\output.txt", true); // true表示在文件末尾追加
    BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(fos));
    for (String result : resultList) {
      bw.write(result);
      bw.newLine();
      bw.flush();
    }
    bw.close();
    fos.close();
  }
}
